﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PMS.Logic.BD
{
    public partial class CompanyAjax : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String methodName = Request["method"];
            Type type = this.GetType();
            MethodInfo method = type.GetMethod(methodName);
            if (method == null) throw new Exception("method is null");
            try
            {

                method.Invoke(this, null);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 获取客户信息
        /// </summary>
        public void GetCompanyList()
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            Expression<Func<BD_CompanyInfo, bool>> exp = SearchExpression.FindByGroup<BD_CompanyInfo>(listSearch);
            string OrderId = OperateContext.Sort == null ? "ComName" : OperateContext.Sort;
            int count = 0;
            var list = DALSession.Current.BD_CompanyInfoDAL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, OrderId, true);
            count = DALSession.Current.BD_CompanyInfoDAL.GetCount(exp);
            Response.Write(JsonHelper.toPageJson(list, count));
        }

        /// <summary>
        /// 修改客户信息
        /// </summary>
        public void EditCompany()
        {
            int ID = Commons.String2Int32(Request.Form["ID"]);
            BD_CompanyInfo perNew = DALSession.Current.BD_CompanyInfoDAL.GetListBy(d => d.ID == ID).FirstOrDefault();
            ModelTools.Form2EditModel(Request.Form, perNew);
            DALSession.Current.BD_CompanyInfoDAL.ModifyAllModel(perNew);
            DALSession.Current.BD_CompanyInfoDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "保存成功"));
        }

        /// <summary>
        /// 添加客户信息
        /// </summary>
        public void AddCompany()
        {
            BD_CompanyInfo per = new BD_CompanyInfo();
            ModelTools.Form2EditModel(Request.Form, per);
            int count = DALSession.Current.BD_CompanyInfoDAL.GetCount(cp=>cp.ComName.Equals(per.ComName));
            if (count > 0)
            {
                Response.Write(JsonHelper.toDefaultMsg(false, "重复添加客户！"));
            }
            else {
                DALSession.Current.BD_CompanyInfoDAL.Add(per);
                DALSession.Current.BD_CompanyInfoDAL.SaveChanges();
                Response.Write(JsonHelper.toDefaultMsg(true, "保存成功"));
            }
        }

        /// <summary>
        /// 删除客户信息
        /// </summary>
        public void DelCompany()
        {
            //  int Id = Commons.String2Int32(Request.Form["Id"]);
            if (Request.Form["ID"] == null)
            {
                Response.Write(JsonHelper.toDefaultMsg(false, "删除失败"));
                return;
            }
            int Id = Convert.ToInt32(Request.Form["ID"].ToString());
            DALSession.Current.BD_CompanyInfoDAL.DelBy(p => p.ID == Id);
            DALSession.Current.BD_CompanyInfoDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "删除成功"));
        }

        /// <summary>
        /// 获取所有的客户名称列表
        /// </summary>
        public void GetAllCompanyNameList()
        {
            string q = Request.Form["q"];
            if (null == q || "" == q)
            {
                Response.Write(JsonHelper.toJson(DALSession.Current.BD_CompanyInfoDAL.GetListBy().Select(p => new { value = p.ID, text = p.ComName })));
            }else {
                Response.Write(JsonHelper.toJson(DALSession.Current.BD_CompanyInfoDAL.GetListBy(c=>c.ComName.Contains(q)).Select(p => new { value = p.ID, text = p.ComName })));
            }           
        }

        /// <summary>
        /// 获取所有客户的所有信息列表
        /// </summary>
        public void GetAllCompanyInfo()
        {
            string q = Request.Form["q"];
            if (q == null)
            {
                var list = DALSession.Current.BD_CompanyInfoDAL.GetListBy();
                Response.Write(JsonHelper.toPageJson(list, list.Count()));
            }
            else
            {
                var list = DALSession.Current.BD_CompanyInfoDAL.GetListBy(r => r.ComName.Contains(q));
                Response.Write(JsonHelper.toPageJson(list, list.Count()));
            }
            //Response.Write(JsonHelper.toJson(DALSession.Current.BD_CompanyInfoDAL.GetListBy(c=>c.ID!=null)));
        }

    }
}